﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;


public class wxMiniProgramLogin : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string openid = context.Request.Headers["X-ACCESS-USER"];//Header中的token


        //   paramJson: {"name":"李锋55","iphone":"13378719128","addre":"12lou","isactive":1,"title":"昆明市人民政府","address":"云南省昆明市呈贡区春融东路与朝云街300米路南","location":{"lat":24.880131,"lng":102.832802},"addname":{"adcode":"530114","province":"云南省","city":"昆明市","district":"呈贡区"},"id":"1"}

        var xxx=HttpContext.Current.Request.Form["paramJson"];
        JObject jo = (JObject)JsonConvert.DeserializeObject(xxx);

        string name = jo["name"].ToString();
        string iphone = jo["iphone"].ToString();

        string   title= jo["title"].ToString();
        string   address= jo["address"].ToString();
        string   location= jo["location"].ToString();
        string   UAProvance= jo["addname"]["province"].ToString();
        string   UACity= jo["addname"]["city"].ToString();
        string   UADistrict= jo["addname"]["district"].ToString();

        string addre = jo["addre"].ToString();
        string isactive = jo["isactive"].ToString();
        string id = jo["id"].ToString();

        if (string.IsNullOrEmpty(openid))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }



        var code = 1;
        var msg = "编辑地址";
        var time = DateTime.Now;

        int _int = int.Parse(id);

        using (Entities bll = new Entities())
        {
            S_UADDRESS  x = new S_UADDRESS();
            if (_int != 0)
            {
                x = bll.S_UADDRESS.SingleOrDefault(p=>p.UAID==_int);
            }

            x.Uopenid = openid;
            x.UAState = isactive;
            x.UPeopleName = name;
            x.UAPhone = iphone;
            x.UAProvance = UAProvance;
            x.UACity = UACity;
            x.UADistrict = UADistrict;
            x.UAddress = address;
            x.UDetailAddress = addre;
            x.Ulatng = location;
            x.UAddName = title;
            bll.SaveChanges();

            if (_int == 0)
            {
                bll.S_UADDRESS.Add(x);

            }
            bll.SaveChanges();
            id = x.UAID.ToString();
            context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,id}));

            HttpContext.Current.Response.End();
        }

    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}